<?php

class ZFDulcet_Marknet_Webapp_Contato extends Zend_Http_Client {

	const URI = 'http://contato.webapp.marknet.com.br/';

	private $_emailAction;
	private $_emailBody;
	private $_emailFrom;
	private $_emailId;
	private $_emailReplyTo = array();
	private $_emailSubject;
	private $_emailTo;

	/**
	 *
	 * @param string $action 
	 */
	public function __construct($id, $action = 'send') {
		parent::__construct();

		$this->setUri(self::URI);
		$this->setEmailAction($action);
		$this->setEmailId($id);
	}

	public function addReplyTo($email, $name = '') {
		$this->_emailReplyTo[] = array('email' => $email, 'name' => $name);
	}
	
	public function clearReplyTo() {
		$this->_emailReplyTo = array();
	}

	/**
	 * Define o action
	 *
	 * @param string $action 
	 */
	public function setEmailAction($action) {
		$this->_emailAction = $action;
	}

	public function setEmailBody($body) {
		$this->_emailBody = $body;
	}

	public function setEmailFrom($email, $name = '') {
		$this->_emailFrom = array('email' => $email, 'name' => $name);
		$this->_emailReplyTo = array(0 => $this->_emailFrom);
	}

	public function setEmailId($id) {
		$this->_emailId = $id;
	}

	/**
	 * Define um assunto para o email
	 *
	 * @param string $subject 
	 */
	public function setEmailSubject($subject) {
		$this->_emailSubject = $subject;
	}

	/**
	 * Define um destinatario
	 * 
	 * @param string $to 
	 */
	public function setEmailTo($email, $name = '') {
		$this->_emailTo = array('email' => $email, 'name' => $name);
	}

	/**
	 *
	 * @return boolean
	 */
	public function send() {
		$this->setParameterPost('id', $this->_emailId);
		$this->setParameterPost('action', $this->_emailAction);
		
		$this->setParameterPost('emailSubject', $this->_emailSubject);
		$this->setParameterPost('emailTo', $this->_emailTo);
		$this->setParameterPost('emailFrom', $this->_emailFrom);
		$this->setParameterPost('emailReplyTo', $this->_emailReplyTo);
		$this->setParameterPost('emailBody', $this->_emailBody);
		
		$response = $this->request(Zend_Http_Client::POST);
		
		switch($response->getHeader('Content-type')) {
			case 'application/json':
				$jsonDecoded = json_decode($response->getBody(), 1);
				return ($jsonDecoded['error'] == 0);
				break;
		}
		
		return false;
	}

}